/**
 * Created by ZFL on 2018/2/8.
 */
/**
 * Created by admin on 2016/12/3.
 */
function PageInit(){

    pageInfo();
}
var mallStoreId= $.getPara('mallStoreId');   //店铺id
var userTalentId= $.getPara('baUserId');

//加载领取优惠券信息页面
function pageInfo() {
    $.ajaxHandler({
        model:function () {
            var obj ={};
            if($.getPara('promotionDetailId')){
                obj.promotionDetailId=$.getPara('promotionDetailId');
            }
            if($.getPara('baUserId')){
                obj.baUserId=$.getPara('baUserId')
            }
            if($.getPara('mallStoreId')){
                obj.storeId=$.getPara('mallStoreId');
            }
            console.log(obj)
            return obj;
        },
        visibleTime: 500,
        approve: true,
        api:"/yg_baijiaplatform/baPromotion/storePromotionDetail",
        success:function (result) {
            //console.log(result);
            var data = result.data;
            if (result.code == 10000) {
                if(data.type==2){//2.通用券3.品牌券.4.预售券 5.商品券
                    $(".coupon-wrap").attr("class","coupon-wrap coupon-general");
                    $(".coupon-wrap p").text("全商品可用");
                }else if(data.type==3){//3.品牌券
                    $(".coupon-wrap").attr("class","coupon-wrap coupon-brand");
                    if(data.brandName&&data.brandName!=""){
                        $(".coupon-wrap p").text("仅限"+data.brandName+'使用');
                    }
                }else if(data.type==5){//5.商品券
                    $(".coupon-wrap").attr("class","coupon-wrap coupon-goods");
                    if(data.goodsName &&　data.goodsName!=""){
                        $(".coupon-wrap p").text("仅限"+data.goodsName+'使用');
                    }
                }
                if(data.isDiscount==1){//是否折扣券  1折扣券  0现金券
                    $(".coupon-wrap h2").html('<span>'+divideNumber(data.discount,10)+'</span>折<em>满'+divideNumber(data.limited,100)+'元使用</em>');
                }else{
                    $(".coupon-wrap h2").html('<span>'+divideNumber(data.amount,100)+'</span>元<em>满'+divideNumber(data.limited,100)+'元使用</em>');
                }
                //if(data.storeUserPicture.toString().indexOf("http")<=-1){
                //    $(".user-pic img").attr("src",$.imgApi()+""+data.storeUserPicture);
                //}else{
                //    $(".user-pic img").attr("src",data.storeUserPicture);
                //}
                //data.storeName='南京西路店';
                //data.nickName='甜甜';
                //data.isStore=1;
                //data.picture='http://oh3cohi55.bkt.clouddn.com/images/uploadImgForm/201706301037543474.jpg';
                if(data.roleType==1){//店员
                    $(".identity").text("店员");
                }else if(data.roleType==2){//店长
                    $(".identity").text("店长");
                }
                //if(data.storeQRcode.toString().indexOf("http")<=-1){
                //    $(".er-pc img").attr("src",$.imgApi()+""+data.storeQRcode);
                //}else{
                //    $(".er-pc img").attr("src",data.storeQRcode);
                //}
                $(".user-pic img").attr("src",data.picture);
                $(".mb-address").text(data.storeName);//店铺名字
                $(".monicker").text(data.nickName);//昵称
                mallStoreId=data.storeId;
            } else {
               YDUI.dialog.toast(result.message,1000)
            }
        }
    });
}
//领取优惠券
function immediately() {
    if( JSON.parse($.getStorage('userData')).userName.length<11){
        if($(".phone").val()==""){
            $.toast("手机号码不能为空","text");
        }else if($(".phone").val().length!==11){
            $.toast("手机号码格式不正确","text");
        }else if($(".code").val()==""){
            $.toast("验证码不能为空","text");
        }else{
            bindPhone();  //绑定手机号
        }
    }else{
        coupon();
    }
}
function coupon() {
    $.ajaxHandler({
        model: function () {
            var obj = {};
           //obj.storeId=mallStoreId;
            if(com_obj.promotionDetailId){
                obj.promotionDetailId=com_obj.promotionDetailId;
            }else if($.getPara("promotionDetailId")!=""){
                obj.promotionDetailId=$.getPara("promotionDetailId");
            }
            //if($.getPara('baUserId')!=''){
            //    obj.baUserId  = $.getPara('baUserId');//分享人id
            //    $.setStorage("talentId",parseFloat($.getPara("baUserId")));
            //}else if($.getStorage("talentId")){
            //    obj.storeUserId  = $.getStorage("talentId");
            //}else if(com_obj.talentId){
            //    obj.storeUserId  = com_obj.talentId;
            //}
            obj.userId=com_obj.userId;

            console.log(obj)
            return obj;
        },
        visibleTime: 500,
        approve: true,
        api:"yg_baijiaplatform/baPromotion/coupons",
        success: function (result) {
            //console.log(result);
            if (result.code == 10000) {
               $('#fullbg').show();
                $('.receviedCoupon').show();
            }else if(result.code==30109){
                YDUI.dialog.toast("您已经领取过了",1000);
            }else if(result.code==20000){
                YDUI.dialog.toast("领取失败",1000);
            }else if(result.code==30109){
                //$.toptip("您已经拥有相同优惠券了","warning");
                YDUI.dialog.toast("您已经拥有相同优惠券了",1000);
            }else{
                //$.toptip(result.message,"warning");
                YDUI.dialog.toast(result.message,1000);
            }
        }
    });
}

//点击遮罩
$('#fullbg').click(function(){
    $('#fullbg').hide();
    $('.receviedCoupon').hide();
})
//发送验证码
function getCode() {
    if($(".phone").val()==""){
        YDUI.dialog.toast("手机号码不能为空",1000);
    }else if($(".phone").val().length!==11){
        YDUI.dialog.toast("手机号码格式不正确",1000);
    }else {
        sendCode();
    }
}
function sendCode(){
    $(".but").attr("disabled","disabled");
    $.ajaxHandler({
        model:function(){
            var obj ={};
            obj.mobile =$(".phone").val();
            console.log(obj)
            return obj;
        },
        visibleTime: 100,
        dataType: "json",
        api:"yg_baijiaplatform/CheckCode/sendVerificationCode",
        success:function (result) {
            //console.log(result);
            var data = result.data;
            if (result.code == 10000) {
                YDUI.dialog.toast("短信发送成功",'success',1000);
                SendTime(".but");
            } else {
                YDUI.dialog.toast("短信发送成功",1000);
            }
        }
    });
}
var wait = 60;//短信时间
function SendTime(parameter){
    var Send = $(""+parameter+"");
    if (wait == 0) {
        Send.val("获取验证码");
        wait = 60;
        $(".but").removeAttr("disabled");
    } else {
        Send.val("重新发送(" + wait + ")s");
        wait--;
        setTimeout(function() {
            SendTime(parameter);
        },1000);
    }
}

//绑定手机号
function bindPhone(){
    $.ajaxHandler({
        model:function(){
            var obj={};
            obj.mobile =$(".phone").val();
            obj.undefinedId= $.getStorage('userOpenid');
            obj.undefinedType=2;
            obj.validCode=$('.code').val();
            console.log(obj)
            return obj;
        },
        visibleTime: 100,
        dataType: "json",
        api:"yg_baijiaplatform/unDefinedUser/getUndefinedUserAdd",
        success:function(result){
            if(result.code==10000){
                var data=result.data;
                saveInfo(data);
                coupon();   //领取优惠券
            }
        }

    })
}


//进入微商城
function goMall(){
    window.location.href= $.wxApi()+'h5_yd/?mallStoreId='+mallStoreId+'&userTalentId='+userTalentId+'&status=0';
}